package com.njust.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njust.common.ServerRespone;
import com.njust.dataobject.*;
import com.njust.enums.RankColor;
import com.njust.enums.ZdType;
import com.njust.mapper.TrainDataMapper;
import com.njust.mapper.TrainInfoMapper;
import com.njust.service.SysParamService;
import com.njust.service.TableService;
import com.njust.utils.DateUtil;
import com.njust.utils.PageUtils;
import com.njust.utils.StringUtil;
import com.njust.vo.PageVO.PageResultVo;
import com.njust.vo.TalbeVO.DetailVO;
import com.njust.vo.TalbeVO.TableVO;
import com.njust.vo.TrainParamVO.TrainParamVO;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @date 2018/3/12 下午2:03
 */
@Service
@Slf4j
public class TableServiceImpl implements TableService {

    @Autowired
    private TrainInfoMapper trainInfoMapper;

    @Autowired
    private TrainDataMapper trainDataMapper;

    @Autowired
    private SysParamService sysParamService;

    @Override
    public ServerRespone showDataByTable(Date startTime, Date endTime, Integer page, Integer size) {

        log.info("前台传过来的json:{},{}",DateUtil.Date2String(startTime),DateUtil.Date2String(endTime));
        List<TableVO> tableVOS=new ArrayList<>();
        List<TrainInfo> trainInfos = trainInfoMapper.findByStateandTrainDate(startTime, endTime);

        if(trainInfos.size()==0){
            return ServerRespone.createByErrorMessage("无数据");
        }
        List<Long> trainOnlyIdList=new ArrayList<>();
        for (TrainInfo trainInfo:trainInfos){
            trainOnlyIdList.add(trainInfo.getTrainOnlyid());
        }


        TrainDataExample trainDataExample=new TrainDataExample();
        TrainDataExample.Criteria criteria1 = trainDataExample.createCriteria();

        criteria1.andTrainOnlyidIn(trainOnlyIdList);
        PageHelper.startPage(page,size);
        List<TrainData> trainData = trainDataMapper.selectByExample(trainDataExample);
        PageInfo<TrainData> pageInfo=new PageInfo<>(trainData);

        if(trainData.size()==0){
            return ServerRespone.createByErrorMessage("无数据");
        }

        ServerRespone serverRespone = sysParamService.showParam();
        TrainParamVO trainParam = (TrainParamVO)serverRespone.getData();
        if(trainParam==null){
            return ServerRespone.createByErrorMessage("请先设置阈值");
        }


        for(TrainInfo trainInfo:trainInfos ){
            for (TrainData td:pageInfo.getList()){
                if(td.getTrainOnlyid().equals(trainInfo.getTrainOnlyid())){
                    //进行数据的封装
                    assembleTableData(tableVOS, trainParam, trainInfo, td);
                }else {
                    continue;
                }
            }
        }
        PageResultVo pageResultVo = new PageResultVo();
        PageUtils.pageUtil(pageInfo,pageResultVo);
        pageResultVo.setList(tableVOS);
        return ServerRespone.createBySuccess(pageResultVo);
    }

    @Override
    public void assembleTableData(List<TableVO> tableVOS, TrainParamVO trainParam, TrainInfo trainInfo, TrainData td) {
        TableVO tableVO=new TableVO();
        Map<String,DetailVO> contentMap=new HashMap<>();
        tableVO.setTrainId(trainInfo.getTrainId());
        tableVO.setTrainOnlyid(trainInfo.getTrainOnlyid());
        tableVO.setTrainState(trainInfo.getTrainState());
        tableVO.setTrainDate(DateUtil.Date2String(trainInfo.getTrainDate()));
        DetailVO jfgdetailVO=new DetailVO();
        if(td.getJfgAlarm()>0){
            jfgdetailVO.setColor(RankColor.ALARM.getCode());
        }else if(td.getJfgWarn()>0){
            jfgdetailVO.setColor(RankColor.WARN.getCode());
        }else {
        }
        jfgdetailVO.setContent(StringUtil.alarmandwarn(td.getJfgAlarm(),td.getJfgWarn()));
        contentMap.put(ZdType.JFG.getMsg(),jfgdetailVO);
        DetailVO fzdetailVO=new DetailVO();
        if(td.getFzAlarm()>0){
            fzdetailVO.setColor(RankColor.ALARM.getCode());
        }else if(td.getFzWarn()>0){
            fzdetailVO.setColor(RankColor.WARN.getCode());
        }else {
        }
        fzdetailVO.setContent(StringUtil.alarmandwarn(td.getFzAlarm(),td.getFzWarn()));
        contentMap.put(ZdType.FZ.getMsg(),fzdetailVO);
        DetailVO fzyzdetailVO=new DetailVO();
        if(td.getFzyzAlarm()>0){
            fzyzdetailVO.setColor(RankColor.ALARM.getCode());
        }else if(td.getFzyzWarn()>0){
            fzyzdetailVO.setColor(RankColor.WARN.getCode());
        }else {
        }
        fzyzdetailVO.setContent(StringUtil.alarmandwarn(td.getFzyzAlarm(),td.getFzyzWarn()));
        contentMap.put(ZdType.FZYZ.getMsg(),fzyzdetailVO);

        //峭度
        DetailVO qdetailVO=new DetailVO();
        if(td.getQdAlarm()>0){
            qdetailVO.setColor(RankColor.ALARM.getCode());
        }else if(td.getQdWarn()>0){
            qdetailVO.setColor(RankColor.WARN.getCode());
        }else {
        }
        qdetailVO.setContent(StringUtil.alarmandwarn(td.getQdAlarm(),td.getQdWarn()));
        contentMap.put(ZdType.QD.getMsg(),qdetailVO);

        //峭度因子
        DetailVO qdyzdetailVO=new DetailVO();
        if(td.getQdyzAlarm()>0){
            qdyzdetailVO.setColor(RankColor.ALARM.getCode());
        }else if(td.getQdyzWarn()>0){
            qdyzdetailVO.setColor(RankColor.WARN.getCode());
        }else {
        }
        qdyzdetailVO.setContent(StringUtil.alarmandwarn(td.getQdyzAlarm(),td.getQdyzWarn()));
        contentMap.put(ZdType.QDYZ.getMsg(),qdyzdetailVO);
         //设置轴温的值，这里需要注意,设置的与电机一致需要修改
         //轴温
        DetailVO zwdetailVO=new DetailVO();
        //大于
        if(td.getZxTemp().compareTo(trainParam.getZwAlarm())==1){
            zwdetailVO.setColor(RankColor.ALARM.getCode());
        } else if(td.getZxTemp().compareTo(trainParam.getZwWarn())==1 && td.getZxTemp().compareTo
                (trainParam.getZwAlarm())==-1){
                 zwdetailVO.setColor(RankColor.WARN.getCode());
        }
        zwdetailVO.setContent(td.getZxTemp().toString());
        contentMap.put("ZW",zwdetailVO);
        //电机温度
        DetailVO motordetailVO=new DetailVO();


        if(td.getMotorTemp().compareTo(trainParam.getMotortempAlarm())==1){
            motordetailVO.setColor(RankColor.ALARM.getCode());
        } else if(td.getMotorTemp().compareTo(trainParam.getMotortempWarn())==1 && td.getMotorTemp().compareTo
                (trainParam.getMotortempAlarm())==-1){
            motordetailVO.setColor(RankColor.WARN.getCode());
        }

        motordetailVO.setContent(td.getMotorTemp().toString());
        contentMap.put("MOTOR",motordetailVO);

        /**
         * 需要添加两个温升值及其状态
         */
        //轴温温升并设置其状态
        DetailVO zwTempIncVO = new DetailVO();
        if(td.getZxTempinc().compareTo(trainParam.getZwincWarn())==1){
            zwTempIncVO.setColor(RankColor.ALARM.getCode());
        } else if(td.getZxTempinc().compareTo(trainParam.getZwincWarn())==1 && td.getZxTempinc().compareTo
                (trainParam.getZwAlarm())==-1){
            zwTempIncVO.setColor(RankColor.WARN.getCode());
        }
        zwTempIncVO.setContent(td.getZxTempinc().toString());
        contentMap.put("ZWINC",zwTempIncVO);

        //电机温度温升并设置其状态
        DetailVO motorTempIncVO = new DetailVO();
        if(td.getMotorTempinc().compareTo(trainParam.getMotortempincWarn())==1){
            motorTempIncVO.setColor(RankColor.ALARM.getCode());
        } else if(td.getMotorTempinc().compareTo(trainParam.getMotortempincWarn())==1 && td.getMotorTempinc().compareTo
                (trainParam.getMotortempincAlarm())==-1){
            motorTempIncVO.setColor(RankColor.WARN.getCode());
        }
        motorTempIncVO.setContent(td.getMotorTempinc().toString());
        contentMap.put("MOTOTTEMPINC",motorTempIncVO);
        //上面已经添加

        tableVO.setDetailVOMap(contentMap);
        tableVOS.add(tableVO);
    }
}
